#include "hiThread.h"

static void *thread_fun(void *);

HiThread::HiThread() : on_thread(false), is_exit(true) {}

void HiThread::start(const RUN_FUN &fun) {
    this->run_fun = fun;
    pthread_create(&thread, NULL, thread_fun, this);
}

void HiThread::stop() {
    if (!this->on_thread) {
        return;
    }
    this->is_exit = true;
    pthread_join(thread, NULL);
    thread = 0;
}

void HiThread::run() {
    this->on_thread = true;
    this->is_exit = false;
    this->run_fun();
    this->on_thread = false;
}

bool HiThread::want_exit() { return this->is_exit; }

static void *thread_fun(void *arg) {
    HiThread *impl = static_cast<HiThread *>(arg);
    impl->run();
    return NULL;
}